from datetime import datetime

from devtools import debug
from pydantic import (
    BaseModel,
    Field, ValidationError,
)

import utils.enum_utils as enum


class UserVO(BaseModel):
    username: str = Field(..., title="用户名", min_length=3, max_length=20)

    nickname: str = Field(..., title="昵称", min_length=3, max_length=20)

    gender: enum.Gender = Field(..., title="性别")

    last_active: datetime = Field(..., title="最近活跃时间")

    code: str = Field(..., title="编号", min_length=20, max_length=20)
    code1: str = Field(..., title="编号", min_length=20, max_length=20)


# 定义自定义错误消息字典
error_messages = {
    "value_error.missing": "缺少必要的值",
    "value_error.extra": "包含多余的值",
    "value_error.any_str.min_length": "字符串长度过短",
    "value_error.any_str.max_length": "字符串长度过长",
    # 添加其他需要的错误消息
}

debug(UserVO.model_fields)

try:
    data = {
        "username": "admin",
        "nickname": "admin",
        "gender": enum.Gender.value,
        "last_active": datetime.now(),
        "code": "123456789012345678901234567890",
        "code1": "1234567890",
    }
    user = UserVO(**data)

except ValidationError as e:
    error_detail = e.errors()
    debug(error_detail)
    translated_errors = []

    for error in error_detail:
        msg = error["msg"]
        loc = error["loc"]

        if msg in error_messages:
            msg = error_messages[msg]

        translated_errors.append({loc: msg})

    # 返回中文错误消息
    debug(translated_errors)
